<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<html>
<head>
    <title>_库位成本配置</title>
    
	
	<script type="text/javascript">
		var tables_main = null;
		$(function(){
			//初始化dataTables-plan
	        tables_main=$("#table_main").dataTable({
	           "bServerSide":true,
	           "sAjaxSource":'${ctx}/stockTypeCostConfig/findStockTypeCostConfig',
	           "fnServerData": retrieveData_main,
	           "bFilter": false,
	           "bSort": false,
	           "sZeroRecords": "查询无记录！",
	           "bProcessing": true,
	           "oLanguage": {
	               "sUrl": '${ctx}/static/v2/js/plugins/datatables/cn.txt'
	            },
	            "aoColumns": [
					{"mData": 'warehouseCode'},
					{"mData": 'stockType'},
					{"mData": 'stockName'},
					{"mData": 'stockCost'},
					{"mData": 'remark'},
					{"mData": 'createTime',"render": function (obj) {
                        var time=obj.time;
                        var date = new Date(time);
                        return date.Format("yyyy-MM-dd");
                     }},
					{"mData": 'createBy'},
					{"mData": 'id',"render": function (obj) {
						var id=obj;
						var str = '<a href="#configWindow" onclick="findById('+id+');" role="button"  class="btn btn-primary" data-toggle="modal">修改</a>&nbsp;';
						str += '<a href="#configCustomer" onclick="stockTypeAssign('+id+');" role="button"  class="btn btn-primary" data-toggle="modal">库位归类</a>&nbsp;';
						str += '<button class="btn btn-danger" onclick="delRow('+id+')" type="button">删除</button>';
                        return str;
                     }}
	           ]
	        });
		});
		function delRow(id){
			jConfirm("确定删除配置信息?","温馨提醒",function(result){
		    	if(result){
					$.ajax({
						type:'post',
						url:'${ctx}/stockTypeCostConfig/deleteStockTypeCostConfig',
						data:{
							id:id
						},
						dataType:'json',
						success:function(data) {
							var msg=data.msg;
							if(msg=='success'){
								jAlert("删除成功!","温馨提示",function(){
									tables_main.fnDraw();
								});
							}else{
								jAlert("删除失败!","温馨提示",function(){
									tables_main.fnDraw();
								});
							}
						},
						error:function() {
							alert("网络繁忙，请稍后重试...");
						}
			       });
	    		}
	      	});
		}
		
		var stockConfigId = null;
		function stockTypeAssign(id){
				stockConfigId = id;
	    		var unAssignTempString = "";
	    		var assignTempString = "";
	    		$("#unAssign").html(unAssignTempString);
	    		$("#assign").html(assignTempString);
	    		$.ajax({
	    			type : 'post',
	    			url : '${ctx}/stockTypeCostConfig/findMappingById',
	    			data : 'id=' + id,
	    			dataType : 'json',
	    			success : function(data) {
	    				if (data.status == "success") {
	    					var assignList = data.assignList;
	    					var unAssignList = data.unAssignList;
	    					for(var i = 0 ; i<unAssignList.length;i++) {
		    					unAssignTempString += "<option value='"+unAssignList[i].stockCode+"'>"+unAssignList[i].stockCode+"</option>";
	    					}
	    					//已分配
	    					for(var j = 0 ; j<assignList.length;j++) {
	    						assignTempString += "<option value='"+assignList[j].stockCode+"'>"+assignList[j].stockCode+"</option>";
	    					}
	    					
	    					
	    					$("#unAssign").html(unAssignTempString);
	    					$("#assign").html(assignTempString);
	    					
	    				} else {
	    					jAlert("联系管理员！","温馨提示");
	    				}
	    			},

	    			error : function() {
	    				jAlert("联系管理员！","温馨提示");
	    			}
	    		});
	    	
	    }
		
		function findById(id) {
			$("#edi_id").val("");
			$("#edi_warehouseCode").val("");
			$("#edi_stockType").val("");
			$("#edi_stockName").val("");
			$("#edi_stockCost").val("");
			$("#edi_remark").val("");
			$("#edi_warehouseCode").trigger("chosen:updated");
			if(id){
				$.ajax({
					type:'post',
					url:'${ctx}/stockTypeCostConfig/findById',
					data:{
						id:id
					},
					dataType:'json',
					success:function(data) {
						var msg=data.msg;
						if(msg=='success'){
							var stcc=data.stcc;
							if(stcc){
								$("#edi_id").val(stcc.id);
								$("#edi_warehouseCode").val(stcc.warehouseCode);
								$("#edi_stockType").val(stcc.stockType);
								$("#edi_stockName").val(stcc.stockName);
								$("#edi_stockCost").val(stcc.stockCost);
								$("#edi_remark").val(stcc.remark);
								$("#edi_warehouseCode").trigger("chosen:updated");
							}else {
								jAlert("信息获取失败!","温馨提示",function(){
									
								});
							}
						}else{
							jAlert("信息获取失败!","温馨提示",function(){
								
							});
						}
					},
					error:function() {
						alert("网络繁忙，请稍后重试...");
					}
		       });
			}
		}
		
		function validate(num)
		{
		  var reg = /^\d+(?=\.{0,1}\d+$|$)/
		  if(reg.test(num)) return true;
		  return false ;  
		} 
		
		function saveMapping() {
			if(!stockConfigId) {
	   			jAlert("操作异常","温馨提示");
	   		}
	   		var assignInfoStr = "";
	   		$('#assign option').each(function(i,o){
	   			assignInfoStr += $(o).val()+",";
	   		});
	   		$.ajax({
	   			type : 'post',
	   			url : '${ctx}/stockTypeCostConfig/saveMapping',
	   			data : 'assignStockCodes=' + assignInfoStr+"&stockConfigId="+stockConfigId,
	   			dataType : 'json',
	   			success : function(data) {
	   				if (data.msg == "success") {
	   					jAlert("保存成功！","温馨提示",function(){
	   						$("#closeId").trigger('click'); 
	   					});
	   				} else {
	   					jAlert("联系管理员！","温馨提示");
	   				}
	   			},
	   			error : function() {
	   				jAlert("联系管理员！","温馨提示");
	   			}
	   		});
		}
		function saveConfig() {
			if(!$("#edi_warehouseCode").val()) {
				jAlert("请选择仓库!","温馨提示",function(){});
				return;
			}
			if(!$("#edi_stockType").val()) {
				jAlert("类型编码必填!","温馨提示",function(){});
				return;
			}
			if(!$("#edi_stockName").val()) {
				jAlert("类型名称必填!","温馨提示",function(){});
				return;
			}
			if(!$("#edi_stockCost").val()) {
				jAlert("成本单价必填!","温馨提示",function(){});
				return;
			}
			if(!validate($("#edi_stockCost").val())) {
				jAlert("成本单价只能大于0的数字!","温馨提示",function(){});
				return;
			}
			
			$.ajax({
                cache: true,	
                type: "POST",
                url:"${ctx}/stockTypeCostConfig/ajaxSave",
                data:$('#editForm').serialize(),
                async: false,
                dataType : 'json',
                error: function(request) {
                	jAlert("服务器异常!","温馨提示",function(){
						tables_main.fnDraw();
					});
                },
                success: function(data) {
                    var msg=data.msg;
					if(msg=='success'){
						jAlert("保存成功!","温馨提示",function(){
							$("#closeId").trigger('click'); 
							tables_main.fnDraw();
						});				
					}else{
						jAlert(data.content,"温馨提示",function(){
// 							$("#closeId").trigger('click'); 
							tables_main.fnDraw();
						});
					}
                }
            });
		}
		function retrieveData_main(sSource, aoData, fnCallback){
			//查询参数
		    
		   var warehouseCode=$("#warehouseCode").val();
		   aoData.push( { "name": "warehouseCode", "value": warehouseCode } );
		   
		   $.ajax({ 
		       "type": "post",
		       "contentType": "application/json", 
		       "url": sSource,    
		       "dataType": "json",   
		       "data": JSON.stringify(aoData),
		       "success": function(resp) {  
	       			fnCallback(resp);
	       		}
		   }); 
		}
		
		function query() {
			tables_main.fnDraw();
// 			tabletables_main("action","${ctx}/warehouseAge/v2_occupiedAreaConfigQuery");
// 			 $("#queryForm").submit();
		}
		
    </script>
</head>

<body>
<div id="fullbg"></div>
<div id="dialog">
	<div>
		<img src="${ctx}/static/022.gif"></img>
	</div>
</div>
<section class="content">
    <!-- 查询条件  start-->
   	<div class="row">
	<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary" >
			    
				<div class="box-header">
					<i class="fa fa-search"></i>
					<h3 class="box-title">搜索栏</h3>
				</div>
				
				<div class="box-body">
				    <form class="form-search" id="queryForm" method="post" action="${ctx}/stockTypeCostConfig/findOccupiedAreaConfig" style="margin-top: 10px;">
					    <!-- 第一行 -->
						<div class="form-group row">
							
							<div class="col-sm-3">
								<div class="input-group col-sm-12">
									<span class="input-group-text"> 仓库： </span> 
									<select class="form-control" id="warehouseCode" name="warehouseCode" >
										<option value=""></option>
										<c:forEach items="${warehouseList}" var="warehouse">
											<option value="${warehouse.groupCode}"
												<c:if test="${warehouseCode eq warehouse.groupCode}">selected=selected</c:if>>${warehouse.groupName}</option>
										</c:forEach>
									</select>
								</div>
							</div>
							
							
						</div>
						<div class="form-group row">
							<div class="col-sm-6">
								<button class="btn btn-info" type="button" onclick="query();">搜索</button>
								<a href="#configWindow" onclick="findById();" role="button"  class="btn btn-primary" data-toggle="modal">添加配置</a>
							</div>
						</div>
					</form>
				</div>

			</div>

	 </div>
	</div>
    <!-- 查询条件 end -->
    
    <!-- 查询结果 start -->
	<div class="form-group row" >
		<div class="col-md-12">
	       <div class="box box-primary">
				 <div class="box-header"></div>
				 <div class="box-body table-responsive">
				     <table id="table_main" class="table table-bordered table-striped">
				         <thead>
				             <tr>
							    <th style="width: 80px;">仓库编码</th>
							    <th style="width: 80px;">库位类型编码</th>
							    <th style="width: 100px;">库位类型名称</th>
							    <th style="width: 60px;">单个库位成本单价</th>
							    <th style="width: 140px;">备注</th>
								<th style="width: 60px;">创建时间</th>
								<th style="width: 80px;">创建人</th>
								<th style="width: 80px;">操作</th>
				             </tr>
				         </thead>
				     </table>
				 </div>
			 </div>
		
		</div>
	</div>
	
	
	<div class="modal fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" style="height:1600px;width:1500;" id="configWindow">
			<div class="modal-dialog" >
			<div class="modal-content" >
			<div class="modal-header">
				<h3 id="myModalLabel">
					修改配置
				</h3>
			</div>
			<div class="modal-body" style="width: 1300;height:400px; overflow: auto;">
				<form class="form-search" id="editForm" method="post" action="" style="margin-top: 10px;">
				<input id="edi_id" name="id" type="hidden"/>
						<table>
							<tr>
								<td align="right">
									仓库：
								</td>
								<td  style="height:40px;width:200px;">
									<div class="form-group row">
										<div class="col-sm-10">
											<div class="input-group col-sm-12">
												<select class="form-control" id="edi_warehouseCode" name="warehouseCode" >
													<option value=""></option>
													<c:forEach items="${warehouseList}" var="warehouse">
														<option value="${warehouse.groupCode}"
															<c:if test="${warehouseCode eq warehouse.groupCode}">selected=selected</c:if>>${warehouse.groupName}</option>
													</c:forEach>
												</select>
											</div>
										</div>
									</div>
								</td>
							</tr>
							<tr>
								<td align="right">
									库位类型编码
								</td>
								<td  style="height:40px;width:250px;">
									<div class="form-group row">
										<div class="col-sm-11">
											<div class="input-group col-sm-12">
												<input style="width:90%;"   class="form-control required"  name="stockType" id="edi_stockType" />
											</div>
										</div>
									</div>
									
								</td>
							</tr>
							<tr>
								<td align="right">
									库位类型名称
								</td>
								<td  style="height:40px;width:250px;">
									<div class="form-group row">
										<div class="col-sm-11">
											<div class="input-group col-sm-12">
												<input style="width:90%;"   class="form-control required"  name="stockName" id="edi_stockName" />
											</div>
										</div>
									</div>
									
								</td>
							</tr>
							<tr>
								<td align="right">
									库位成本单价 (元):
								</td>
								<td  style="height:40px;width:250px;">
									<div class="form-group row">
										<div class="col-sm-11">
											<div class="input-group col-sm-12">
												<input style="width:90%;" class="form-control required"   name="stockCost" id="edi_stockCost" />
											</div>
										</div>
									</div>
								</td>
							</tr>
							<tr>
								<td align="right">
									备注:
								</td>
								<td  style="height:50px;width:350px;">
									<div class="form-group row">
										<div class="col-sm-11">
											<div class="input-group col-sm-12">
												<textArea style="width:90%;" class="form-control required"  name="remark" id="edi_remark" >
												</textArea>
											</div>
										</div>
									</div>
								</td>
							</tr>
						</table>
					</form>
			</div>
			<div class="modal-footer">
				<a href="#" onclick="saveConfig()" role="button"  class="btn btn-primary" data-toggle="modal">
					保存
				</a>
				<button class="btn" id="closeId" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		</div>
	</div>
	
	
	
	
	<script>
   	
   function toAssign() {
		$('#unAssign option:selected').each(function(i,o){
			$("#assign").append($(o));
		});
	}
		
	function toUnAssign() {
		$('#assign option:selected').each(function(i,o){
			$("#unAssign").append($(o));
		});
	}
   </script>
   <div class="modal fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" style="width:1400;" id="configCustomer">
			<div class="modal-dialog" >
			<div class="modal-content" >
			<div class="modal-header">
				<h3 id="myModalLabel">
					库位归类
				</h3>
			</div>
			<div class="modal-body" style="width: 1300;height:300px; overflow: auto;">
				<table class="table table-striped table-bordered table-condensed"
					id="subWarehouseList">
					<thead>
						<tr>
							<th>未归类</th>
							<th></th>
							<th>已归类</th>
						</tr>
					</thead>
					<tr>
						<td>
							<select id="unAssign" size=12 style="width:210px;height:200px;"   class="oldSelectStyle" ondblclick="toAssign()" multiple="multiple"></select>
						</td>
						
						<td align="center" valign="middle">
							<br />
							<br />
							<br />
							<a onclick="toAssign()" role="button"  class="btn btn-primary" data-toggle="modal"> > </a>
							<br />
							<br />
							<a onclick="toUnAssign()" role="button"  class="btn btn-primary" data-toggle="modal"> < </a>
						</td>	
						
						<td>
							<select id="assign" size=12  style="width:210px;height:200px;"   class="oldSelectStyle" ondblclick="toUnAssign()"  multiple="multiple"></select>
						</td>
					</tr>
				</table>
			</div>
			<div class="modal-footer">
				<a href="#" onclick="saveMapping()" role="button"  class="btn btn-primary" data-toggle="modal">
					保存
				</a>
				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		</div>
	</div>
	
	
</section>     
</body>
</html>
